<template>
  <div class="wrap">
    <div class="fly">
      <span>角色列表</span>
      <el-button class="flyc" @click="change(null,{key:1})">添加角色</el-button>
    </div>
    <el-table border :data="tableData" align="center">
      <el-table-column label="序号" type="index" width="50"></el-table-column>
      <el-table-column label="角色名称" width="180">
        <template slot-scope="scope" align="center">
          <div slot="reference" class="name-wrapper">
            <el-tag effect="plain">{{ scope.row.roleName}}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="角色描述" align="center">
        <template slot-scope="scope">
          <el-tag effect="plain">{{scope.row.roleDesc}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="mini"
            @click="change(scope.row.id,{key:2})"
            class="el-icon-edit"
          >编辑</el-button>
          <el-button
            type="text"
            size="mini"
            :key="scope.row.id"
            @click="delpower(scope.row)"
            class="el-icon-delete"
          >删除</el-button>
          <el-button
            type="text"
            size="mini"
            @click="handleDelete(scope.$index, scope.row)"
            class="el-icon-setting"
          >分配权限</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑表单 -->
    <el-drawer size="50%" title="编辑用户" :visible.sync="drawer">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item prop="roleName" label="角色名称">
          <el-input v-model="form.roleName"></el-input>
        </el-form-item>
        <el-form-item prop="roleDesc" label="角色描述">
          <el-input v-model="form.roleDesc"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="createHandler(form)" type="primary">提交</el-button>
          <el-button @click="drawer=false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import { powerlist, delpower, powerchange, addprower } from "@/api/admin.js";
export default {
  data() {
    return {
      form: {
        id: "",
        roleName: "",
        roleDesc: ""
      },
      tableData: [],
      search: "",
      drawer: false,
      out: false,
      itb: 1,
      rules: {
        roleName: [
          { required: true, message: "角色不能为空", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        roleDesc: [
          { required: true, message: "描述不能为空", trigger: "blur" },
          { min: 3, max: 16, message: "长度在 3 到 16 个字符", trigger: "blur" }
        ]
      }
    };
  },
  mounted() {
    // 获取列表
    powerlist().then(res => {
      this.tableData = res.data.data;
    });
  },
  methods: {
    //删除某项
    delpower(power) {
      delpower(power).then(res => {
        if (res.data.meta.status === 200) {
          var idx = this.tableData.findIndex(function(value) {
            return value.id === power.id;
          });
          this.tableData.splice(idx, 1);
        }
      });
    },

    //编辑某项
    createHandler(form) {
      if (this.itb === 1) {
        addprower(form);
        this.$router.go(0);
      } else {
        powerchange(form);
        this.$router.go(0);
      }
    },
    change(idx, ite) {
      this.drawer = true;
      this.form.id = idx;
      this.itb = ite.key;
    }
  }
};
</script>

<style scoped lang="scss">
.wrap {
  overflow: hidden;
  margin-top: 20px;
  background-color:white ;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
  .fly {
    margin-bottom: 10px;
    margin-top: 20px;
    display: flex;
    justify-content: space-between;
    margin-left: 10px;
    margin-right: 10px;
  }
}
.el-icon-delete {
  color: red;
}
.el-icon-setting {
  color: rgb(255, 209, 41);
}
.el-table {
  width: 95%;
  margin: 15px;
}
</style>